﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：账套管理
 */
var selectedRow;
var refreshGirdData;
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var page = {
        init: function () {
            page.bind();
            page.inittree();
            page.initGrid();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!companyId) {
                    learun.alert.warning('请选择机构！');
                    return false;
                }
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加账套',
                    url: config.webportal_server + 'pages/salary/zt/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                    width: 480,
                    height: 350,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑账套',
                        url: config.webportal_server + 'pages/salary/zt/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                        width: 480,
                        height: 350,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'gzzts/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 添加角色成员
            $('#lr_memberadd').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                var loginInfo = learun.clientdata.get(['userinfo']);
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '添加账套成员',
                        url: config.webportal_server + 'pages/salary/zt/selectForm.html?ztId=' + keyValue + '&companyId=' + companyId + '&departmentId=' + loginInfo.departmentId + '&access_token=' + config.getToken().access_token,
                        width: 800,
                        height: 520,
                        callBack: function (id) {
                            return top[id].acceptClick();
                        }
                    });
                }
            });
            // 查看成员
            $('#lr_memberlook').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '查看账套成员',
                        url: config.webportal_server + 'pages/salary/zt/lookForm.html?ztId=' + keyValue + '&companyId=' + companyId + '&access_token=' + config.getToken().access_token,
                        width: 800,
                        height: 520,
                        btn: null
                    });
                }
            });
        },
        inittree: function () {
            $('#companyTree').lrtree({
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                param: {parentId: '0'},
                nodeClick: page.treeNodeClick
            });
            $('#companyTree').lrtreeSet('checkFirst');
        },
        treeNodeClick: function (item) {
            companyId = item.id;
            $('#titleinfo').text(item.text);
            page.search();
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'gzzts/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '账套编号', name: 'code', width: 100, align: 'left'},
                    {label: '账套名称', name: 'name', width: 200, align: 'left'},
                    {
                        label: '更新时间', name: 'updateTime', width: 160, align: 'center',
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "描述", name: "description", index: "description", width: 300, align: "left"}
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑账套',
                            url: config.webportal_server + 'pages/salary/zt/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                            width: 480,
                            height: 350,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


